using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TinyERP.Framework;
using TinyERP.Framework.Controls;
using Bamboo.Core.Data;
using Bamboo.Core.Common.Collection;

using TinyERP.Core.Business;
using TinyERP.Core.DTO;
using TinyERP.Core.View.Controls;
using log4net;

namespace TinyERP.Core.View
{
    public class DepartmentListForm : BaseListForm
    {
		private DataGridViewTextBoxColumn nameColumn;
		private DataGridViewTextBoxColumn descriptionColumn;
        private readonly ILog logger = LogManager.GetLogger(typeof(DepartmentListForm));
		public IDepartmentService DepartmentService 
		{
			get
			{
				return (IDepartmentService) ServiceLocator.GetService("DepartmentServiceProxy");
			}
		}
		

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.nameColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.nameColumn.Width = 200;
            this.descriptionColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
            this.descriptionColumn.Width = 300;
            this.bottomPanel.SuspendLayout();
            this.contentPanel.SuspendLayout();
            this.navigationPanel.SuspendLayout();
            this.SuspendLayout();
            // 
            // searchPanel
            // 
            this.searchPanel.Size = new System.Drawing.Size(809, 37);
            // 
            // bottomPanel
            // 
            this.bottomPanel.Size = new System.Drawing.Size(809, 364);
            // 
            // controlPanel
            // 
            this.controlPanel.Location = new System.Drawing.Point(0, 328);
            this.controlPanel.Size = new System.Drawing.Size(809, 36);
            // 
            // contentPanel
            // 
            this.contentPanel.Size = new System.Drawing.Size(809, 328);
            // 
            // navigationPanel
            // 
            this.navigationPanel.Location = new System.Drawing.Point(0, 298);
            this.navigationPanel.Size = new System.Drawing.Size(809, 30);
            // 
            // paging
            // 
            this.paging.Size = new System.Drawing.Size(809, 30);
            // 
            // nameColumn
            // 
            this.nameColumn.Name = "nameColumn";
            this.nameColumn.Width = 200;
            // 
            // descriptionColumn
            // 
            this.descriptionColumn.Name = "descriptionColumn";
            this.descriptionColumn.Width = 300;
            // 
            // DepartmentListForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.ClientSize = new System.Drawing.Size(809, 401);
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "DepartmentListForm";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.bottomPanel.ResumeLayout(false);
            this.contentPanel.ResumeLayout(false);
            this.navigationPanel.ResumeLayout(false);
            this.navigationPanel.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        
        
        private void SetupDataGridView()
        {
this.dataGridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
			this.nameColumn
			,
			this.descriptionColumn
            });
            
			this.nameColumn.DataPropertyName = "Name";
			this.nameColumn.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Name, TinyERP.Core");
			this.nameColumn.Name = "nameColumn";
			this.nameColumn.Tag = "name";
			this.nameColumn.ReadOnly = true;
			this.nameColumn.SortMode = DataGridViewColumnSortMode.Programmatic;
			this.descriptionColumn.DataPropertyName = "Description";
			this.descriptionColumn.HeaderText = ModuleResourceManager.GetInstance().GetStringFromModuleResourceKey("Description, TinyERP.Core");
			this.descriptionColumn.Name = "descriptionColumn";
			this.descriptionColumn.Tag = "description";
			this.descriptionColumn.ReadOnly = true;
			this.descriptionColumn.SortMode = DataGridViewColumnSortMode.Programmatic;

            this.dataGridView.AutoGenerateColumns = false;
            this.dataGridView.MultiSelect = false;
            this.dataGridView.ModuleAssemblyName = "TinyERP.Core";
            this.dataGridView.AllowUserToOrderColumns = true;
            this.dataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            InitializeSortKeyMap();
        }
        
        private void InitializeSortKeyMap() 
        {
        }

		public DepartmentListForm() : base()
        {
            InitializeComponent();
			SetupDataGridView();
        }
        

		public override void OnNew()
        {
            DepartmentEditInfoForm editForm = new DepartmentEditInfoForm();
            editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            };

            new DialogHelper(editForm).ShowDialog(UIManager.MainScreen);
        }
        

		public override void OnDelete()
        {
			if (this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            DepartmentDTO department = (DepartmentDTO)this.dataGridView.SelectedRows[0].DataBoundItem;
            try 
			{
                if (MessageBox.Show(CommonMessage.GetDeleteConfirmMessage("Department"),
                CommonMessage.GetConfirmationText(), MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
					DepartmentService.DeleteDepartmentByID(department.ID);
					MessageBox.Show(CommonMessage.GetDeleteSuccessMessage("Department"));
					LoadGridData();
				}
			}
			catch(Exception ex) 
			{
                logger.Error(ex);
				MessageBox.Show(CommonMessage.GetCanNotDeleteError("Department"));
			}
        }
        
        
        public override void OnEdit()
        {
            if (this.dataGridView.SelectedRows == null || this.dataGridView.SelectedRows.Count == 0)
            {
                return;
            }
            DepartmentDTO departmentDTO = (DepartmentDTO) this.dataGridView.SelectedRows[0].DataBoundItem;
            DepartmentEditInfoForm editForm = new DepartmentEditInfoForm();
            editForm.IsEditMode = true;
            editForm.ApplyACL(this.AllowEdit, this.AllowDelete, this.AllowCreate);
            editForm.Department = departmentDTO;
			editForm.OnNeedReloadData += delegate(object senderObj)
            {
                LoadGridData();
            };
            new DialogHelper(editForm).ShowDialog(UIManager.MainScreen);
        }
        

        

        
		protected override void LoadGridViewSearchData(IList<SearchCondition> searchConditions, Bamboo.Core.Data.SortDirection sortDirection, string columnName)
        {
			long totalRecord = 0;
            IList departmentList = DepartmentService.SearchDepartment(paging.CurrentPage, paging.PageSize, searchConditions, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            if (departmentList.Count == 0)
            {
                MessageBoxHelper.ShowInformation(AppResourceManager.GetInstance().GetString("ResultNotFound"));
            }
            SortableBindingList<DepartmentDTO> sortableDepartmentList = SortableBindingList<DepartmentDTO>.CreateSortableBindingList(departmentList);
            this.dataGridView.DataSource = sortableDepartmentList;
        }

        protected override void LoadGridViewDefaultData(Bamboo.Core.Data.SortDirection sortDirection, string columnName) 
        {
        	long totalRecord = 0;
            IList departmentList = DepartmentService.GetAllDepartment(paging.CurrentPage, paging.PageSize, columnName, sortDirection, out totalRecord);
            this.paging.TotalRecord = totalRecord;
            SortableBindingList<DepartmentDTO> sortableDepartmentList = SortableBindingList<DepartmentDTO>.CreateSortableBindingList(departmentList);
            this.dataGridView.DataSource = sortableDepartmentList;
        }        

    }
}
